﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HQWeb.SysManage.DictionaryManage
{
    public partial class DictionaryOrderChild : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                int ID = int.Parse(Request.QueryString["ID"]);
                this.BindData(ID);
            }
        }

        public void BindData(int ID)
        {
            BLL.SysManage.T_PUB_DICTIONARY option = new BLL.SysManage.T_PUB_DICTIONARY();
            DataTable dt = null;
             dt = option.GetParentOfChildList(ID);
         
            this.lb_deptList.DataSource = dt;
            lb_deptList.DataTextField = "NAME";
            lb_deptList.DataValueField = "ID";
            this.lb_deptList.DataBind();
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int ID;
            int OrderID;

            BLL.SysManage.T_PUB_DICTIONARY option = new BLL.SysManage.T_PUB_DICTIONARY();

            for (int i = 0; i <= this.lb_deptList.Items.Count - 1; i++)
            {
                ID = int.Parse(this.lb_deptList.Items[i].Value);
                OrderID = i + 1;
                option.UpdateOrderID(ID, OrderID);
            }

            Universal.JS.RunJS("alert('排序成功');window.close();");
            //Response.Write("<script>alert('排序成功');</script>");
        }

        #region 上移
        protected void btn_Up_Click(object sender, EventArgs e)
        {
            if (lb_deptList.SelectedIndex == -1)
            {
                //Page.RegisterStartupScript("alert", "<script language=JavaScript>alert('请选择要上移的结点！！');</script>");
                Universal.JS.RunJS("alert('请选择要上移的结点！！');");
                return;
            }
            int LcCounts = lb_deptList.Items.Count;
            if (lb_deptList.SelectedIndex != 0)
            {
                ListItem lb = new ListItem();
                lb = lb_deptList.Items[lb_deptList.SelectedIndex];
                int SLindex = lb_deptList.SelectedIndex;
                lb_deptList.Items.Remove(lb_deptList.SelectedItem);
                lb_deptList.Items.Insert(SLindex - 1, lb);
            }
        }
        #endregion

        #region 下移
        protected void btn_Down_Click(object sender, EventArgs e)
        {
            if (lb_deptList.SelectedIndex == -1)
            {
                //Page.RegisterStartupScript("alert", "<script language=JavaScript>alert('请选择要下移的结点！！');</script>");
                Universal.JS.RunJS("alert('请选择要下移的结点！！');");
                return;
            }
            int LcCounts = lb_deptList.Items.Count;
            if (lb_deptList.SelectedIndex + 1 != LcCounts)
            {
                lb_deptList.Items.Insert(lb_deptList.SelectedIndex + 2, lb_deptList.SelectedItem);
                lb_deptList.Items.Remove(lb_deptList.SelectedItem);
            }
        }
        #endregion
    }
}